(* ::Package:: *)

SetDirectory["C:\\Projects\\branches\\HMC_core\\output"]


param=OpenRead["..\\input\\params_cts.txt"];
Skip[param,{Number}];
Skip[param,{Number}];
Skip[param,{Number}];
Skip[param,{Number}];
Skip[param,{Number}];
Skip[param,{Number}];
Skip[param,{Number}];
maxIter=Read[param,Number];

Close[param];


base="Zakharov\\";
basedir=SetDirectory[base];
list=FileNames["CTS_diagnosis*.txt"];
plotArray={};
maxIter+=1;


(*list*)


For[p=1,p<=Length[list],p++,
 filedir=basedir<>"\\"<>list[[p]];

 str=OpenRead[filedir];
 cc=Array[c,{maxIter,3}];
 bb=Array[b,{maxIter,2}];
 a={};

 For[i=1,i<=maxIter,i++,
  cc[[i]]=Read[str,{Number,Number,Number}];
 ];

 For[j=1,j<=maxIter,j++,
  For[k=1,k<2,k++,
   bb[[j,k]]=cc[[j,k]];
   bb[[j,k+1]]=cc[[j,k+2]];
  ]
 ];
 (*MatrixForm[bb]*)
 bestValuePos=1;

 For[l=1,l<maxIter,l++,
  If[bb[[bestValuePos,2]]>bb[[l+1,2]],bestValuePos=l+1,
   If[bb[[bestValuePos,2]]<=bb[[l+1,2]],
	 bb=ReplacePart[bb,{l+1}->0],
	 Print["Something has gone wrong at ",l]
   ];
  ];
 ];

 For[n=1,n<=maxIter,n++,
  If[TrueQ[bb[[n]]==0]==True,
   Null,
   AppendTo[a,bb[[n]]]
  ];
 ];

 AppendTo[plotArray,a];
 Close[str];
 Clear[cc,c,bb,b,a,i,j,bestValuePos,l,k,n,filedir]
]


(*MatrixForm[cc]*)


(*MatrixForm[bb]*)


plotArray;


ListPlot[ 
 Table[
  Tooltip[
   plotArray[[i,j]],
   {list[[i]],plotArray[[i,j]]}
  ],
  {i,1,Length[list]},
  {j,1,Length[plotArray[[i]]]}
 ],
 PlotStyle->PointSize[Medium],Joined->True,Mesh->All,AxesOrigin->{0,-1},AxesLabel->{"Function Eval","Distance"},PlotRange->Full
]


Clear[basedir,base,list,maxIter,maxRun,plotArray]
